package com.gongpingjialoan.activity.car;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.main.BaseActivity;
import com.gongpingjialoan.api.API;
import com.gongpingjialoan.data.UserManager;
import com.gongpingjialoan.global.GPJApplication;
import com.gongpingjialoan.network.NetDataJson;
import com.gongpingjialoan.utility.SharedPreferencesUtil;
import com.gongpingjialoan.widget.DateTimePickDailog;
import com.gongpingjialoan.widget.LoadingDialog;

import org.json.JSONObject;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2015/12/28.
 */
public class SeeCarActivity extends BaseActivity implements OnClickListener, NetDataJson.OnNetDataJsonListener {

    private EditText phoneEditText;

    private NetDataJson netWork;

    private LoadingDialog loadingDialog = null;

    private EditText nameEditText;

    private DateTimePickDailog datePickerDialog;

    private String car_id;

    private TextView toseeTextView;

    private String data = "";

    private Button submitButton;

    private LinearLayout mLinearLayout;

    private DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.see_car_layout);
        setTitle("预约看车");
        car_id = getIntent().getStringExtra("car_id");
        loadingDialog = new LoadingDialog(this, "加载中...");
        datePickerDialog = new DateTimePickDailog(this, 7, null);
        phoneEditText = (EditText) findViewById(R.id.phone);
        nameEditText = (EditText) findViewById(R.id.name);
        mLinearLayout = (LinearLayout) findViewById(R.id.appiont_id);
        submitButton = (Button) findViewById(R.id.submit);
        mLinearLayout.setOnClickListener(this);
        submitButton.setOnClickListener(this);
        toseeTextView = (TextView) findViewById(R.id.time);
        String name = (String) SharedPreferencesUtil.get(this, "discount_name", "");
        if (!TextUtils.isEmpty(name)) {
            nameEditText.setText(name);
        }
        if (GPJApplication.getInstance().isLogin()) {
            UserManager mUserManager = new UserManager(this);
            mUserManager = mUserManager.LoadUserInfo();
            phoneEditText.setText(mUserManager.getPhone());
        }
    }


    public void setTime(String str, Date date, String time) {
        String newTime = time.replace("点", "");
        data = df.format(date) + " " + newTime + ":00";
        toseeTextView.setText(str);
    }

    @Override
    public void onClick(View v) {
        if (v == mLinearLayout) {
            datePickerDialog.show();
        } else if (v == submitButton) {
            submit();
        }
    }


    private void submit() {
        if (TextUtils.isEmpty(nameEditText.getText().toString())) {
            Toast.makeText(this, "请输入姓名", Toast.LENGTH_SHORT).show();
            return;
        }
        if (phoneEditText.getText().toString().length() < 11) {
            Toast.makeText(this, "请输入11位手机号码", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(data)) {
            Toast.makeText(this, "请选择正确的时间", Toast.LENGTH_SHORT).show();
            return;
        }

        if (null != netWork) {
            netWork.cancelTask();
        }
        SharedPreferencesUtil.put(this, "discount_name", nameEditText.getText().toString());
        loadingDialog.show();
        if (netWork == null)
            netWork = new NetDataJson(this);
        netWork.setUrl(API.appointdiscount);
        netWork.addParam("phone", phoneEditText.getText().toString());
        netWork.addParam("contact", nameEditText.getText().toString());
        netWork.addParam("promo_car_id", car_id);
        netWork.addParam("date_time", data);
        netWork.addParam("device_code", GPJApplication.getInstance().getDeviceCode());
        netWork.request("post");
    }


    @Override
    public void onDataJsonError(String errorMessage) {
        loadingDialog.dismiss();
        Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show();
    }


    @Override
    public void onDataJsonUpdate(JSONObject json) {
        loadingDialog.dismiss();
        setResult(RESULT_OK);
        finish();
    }
}
